<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/9/19 0019
 * Time: 15:08
 */

namespace app\api\controller\v1\agent;


use app\api\controller\v1\Agent;
use app\currency\service\DateService;
use app\currency\service\OrderRoyalty;
use think\Db;

class TeamProfit extends Agent
{
    /**
     * 收益记录
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function index()
    {
        $cid = $this->request->get('cid');
        empty($cid) && $this->error('cid不能为空！');
        $data = $this->_input([
            'modal' => $this->request->get('modal'),
            'range' => $this->request->get('range'),
        ]);
        in_array($data['modal'],['all','service','bind']) || $data['modal'] = 'all';
        in_array($data['range'],['all','self','team']) || $data['range'] = 'all';
        $group = OrderRoyalty::getRoyaltyListByDay($cid, $data['modal'], $data['range']);
        if($group)
        {
            foreach ($group as &$item) {
                $item['total_amount'] = Db::name('CleanOrderRoyalty')
                    ->whereIn('id',$item['ids'])
                    ->sum('amount');
                $item['list'] = Db::name('CleanOrderRoyalty')
                    ->alias('a')
                    ->leftJoin('cleaner b','a.ccid = b.id')
                    ->leftJoin('member c','a.mid = c.id')
                    ->whereIn('a.id',$item['ids'])
                    ->field('a.*,b.name cleaner_name,b.work_headimg,c.nickname,c.headimg')
                    ->select();
            }
        }
        $this->success('获取收益记录成功！', $group);
    }
    /**
     * 团队收益统计
     * @param $id
     */
    public function read($id)
    {
        $result = [
            'today' => OrderRoyalty::getRoyaltyPrice($id, 'all','all',date('Y-m-d 00:00:00'),date('Y-m-d 23:59:59')),
            'month' => OrderRoyalty::getRoyaltyPrice($id, 'all','all',DateService::beginMonth(),DateService::endMonth()),
            'self_service' => OrderRoyalty::getRoyaltyPrice($id,'service','self'),
            'self_sale' => OrderRoyalty::getRoyaltyPrice($id,'bind','self'),
            'team_service' => OrderRoyalty::getRoyaltyPrice($id,'service','team'),
            'team_sale' => OrderRoyalty::getRoyaltyPrice($id,'bind','team'),
        ];
        $this->success('获取成功！',$result);
    }
}